<?php
/**
 * Created by mq.
 * User: mq
 * Date: 17-9-3
 * Time: 下午10:22
 */
namespace common\controller;

use think\Controller;
use think\Loader;
use think\Cache;
use think\Image;
use think\Db;
use tool\auth;
use http\Env\Request;
use think\File;
use think\Session;


class AdminBase extends Controller {

    function _initialize()
    {
        !is_login() && $this->error('请先登录！', '/admin/login');

        $haveAuth = Session::get('user.auth');
        $super = Session::get('user.super');

        $haveAuth = $haveAuth ? $haveAuth:[];

        if(!$super && !auth::verdict($this->request->instance(), $haveAuth)) $this->error('没有权限！',url('index'));
    }

    function qr($id, $text){
        Loader::import('tool.PhpQrCode',EXTEND_PATH);

        $domain = Cache::get('domain');

        if(!$domain) $domain = 'scan.monicaquartz.com';

        $value = 'http://'.$domain.'/goods/'.$id.'.html';					//二维码内容

        $errorCorrectionLevel = 'L';	//容错级别
        $matrixPointSize = 15;			//生成图片大小

        $fileName = 'goods_'.$id.'.png';
        $filePath = ROOT_PATH.'public/static/images/qr/'.$fileName;

        \QRcode::png($value, $filePath , $errorCorrectionLevel, $matrixPointSize, 2);

        if(isset($text) && $text){
            $image = Image::open($filePath);
            $image->open($filePath)->text($text,ROOT_PATH.'public/static/fonts/SourceHanSansCN-Regular.otf',20,'#000000', 8)->save($filePath);
        }

        return file_exists($filePath) ? true:false;
    }
}